﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FemtonPussel
{
    public partial class Form1 : Form
    {
        private int[,] Board = new int[4, 4];
        private int[,] FinishedBoard = new int[4, 4];
        public Form1()
        {
            InitializeComponent();
            int Number = 1;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Board[i, j] = Number;
                    FinishedBoard[i, j] = Number;
                    Number++;

                }
            }

            Board[3, 3] = 0;
            FinishedBoard[3, 3] = 0;
            //Randomize();
        }

        public void Randomize() // Randomizar genom swap
        {
            Random random = new Random();
            int[,] TempBoard = new int[1, 1];

            for (int i = 0; i < 20000; i++)
            {
                int RandomRow = random.Next(0, 4);
                int RandomColumn = random.Next(0, 4);

                TempBoard[0, 0] = Board[RandomRow, RandomColumn];
                Board[RandomRow, RandomColumn] = Board[0, 0];
                Board[0, 0] = TempBoard[0, 0];
            }

            int zerox;
            int zeroy;
            for (int i = 0; i < 4; i++)       //Sätter sista brickan till 0
            {
                for (int j = 0; j < 4; j++)
                {
                    if (Board[i, j] == 0)
                    {
                        zerox = i;
                        zeroy = j;
                        TempBoard[0, 0] = Board[3, 3];
                        Board[3, 3] = Board[zerox, zeroy];
                        Board[zerox, zeroy] = TempBoard[0, 0];
                    }
                }
            }
        } 



        public bool CheckBoard()
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (Board[i, j] != FinishedBoard[i, j])
                    {
                        return false;
                    }
                }
            }
            return true;
        }



        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            this.BackColor = Color.Bisque;
            SolidBrush AzureBrush = new SolidBrush(Color.Azure);
            Pen BlueViolet = new Pen(Color.BlueViolet, 1);

            Rectangle Rektangel1 = new Rectangle(5, 5, 50, 50); // rad 1
            Rectangle Rektangel2 = new Rectangle(60, 5, 50, 50);
            Rectangle Rektangel3 = new Rectangle(115, 5, 50, 50);
            Rectangle Rektangel4 = new Rectangle(170, 5, 50, 50);

            Rectangle Rektangel5 = new Rectangle(5, 60, 50, 50); // rad 2
            Rectangle Rektangel6 = new Rectangle(60, 60, 50, 50);
            Rectangle Rektangel7 = new Rectangle(115, 60, 50, 50);
            Rectangle Rektangel8 = new Rectangle(170, 60, 50, 50);

            Rectangle Rektangel9 = new Rectangle(5, 115, 50, 50); // rad 3
            Rectangle Rektangel10 = new Rectangle(60, 115, 50, 50);
            Rectangle Rektangel11 = new Rectangle(115, 115, 50, 50);
            Rectangle Rektangel12 = new Rectangle(170, 115, 50, 50);


            Rectangle Rektangel13 = new Rectangle(5, 170, 50, 50); // rad 4
            Rectangle Rektangel14 = new Rectangle(60, 170, 50, 50);
            Rectangle Rektangel15 = new Rectangle(115, 170, 50, 50);
            Rectangle Rektangel16 = new Rectangle(170, 170, 50, 50);


            e.Graphics.FillRectangle(AzureBrush, Rektangel1); // Gör rutor av färgen Azure
            e.Graphics.FillRectangle(AzureBrush, Rektangel2); // Utan kant
            e.Graphics.FillRectangle(AzureBrush, Rektangel3);
            e.Graphics.FillRectangle(AzureBrush, Rektangel4);
            e.Graphics.FillRectangle(AzureBrush, Rektangel5);
            e.Graphics.FillRectangle(AzureBrush, Rektangel6);
            e.Graphics.FillRectangle(AzureBrush, Rektangel7);
            e.Graphics.FillRectangle(AzureBrush, Rektangel8);
            e.Graphics.FillRectangle(AzureBrush, Rektangel9);
            e.Graphics.FillRectangle(AzureBrush, Rektangel10);
            e.Graphics.FillRectangle(AzureBrush, Rektangel11);
            e.Graphics.FillRectangle(AzureBrush, Rektangel12);
            e.Graphics.FillRectangle(AzureBrush, Rektangel13);
            e.Graphics.FillRectangle(AzureBrush, Rektangel14);
            e.Graphics.FillRectangle(AzureBrush, Rektangel15);
            e.Graphics.FillRectangle(AzureBrush, Rektangel16);


            e.Graphics.DrawRectangle(BlueViolet, Rektangel1); // Gör en svart kant
            e.Graphics.DrawRectangle(BlueViolet, Rektangel2);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel3);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel4);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel5);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel6);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel7);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel8);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel9);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel10);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel11);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel12);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel13);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel14);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel15);
            e.Graphics.DrawRectangle(BlueViolet, Rektangel16);


            
            Font font = new Font(FontFamily.GenericSansSerif, 34);
            SolidBrush BlackBrush = new SolidBrush(Color.Black);

            int x;
            int y = 2;
            for(int i = 0; i < 4; i++)
            {
                x = 0;
                for(int j = 0; j < 4; j++)
                {
                    String drawString1 = Board[i,j].ToString();
                    if (Board[i, j] != 0)
                    {
                        e.Graphics.DrawString(drawString1, font, BlackBrush, x, y);
                    }    
                    x += 54;

                }
                y += 55;
            }
           
        }



        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool changed = false;
            if (e.KeyChar == 'a')
            {
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        if (Board[i, j] == 0 && j > 0)
                        {
                            Board[i, j] = Board[i, (j - 1)];
                            Board[i, (j - 1)] = 0;
                            this.Invalidate();
                            changed = true;
                            break;
                        }
                    }
                    if (changed == true) { break; }
                }
            }

            else if (e.KeyChar == 'd')
            {
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        if (Board[i, j] == 0 && j < 3)
                        {
                            Board[i, j] = Board[i, (j + 1)];
                            Board[i, (j + 1)] = 0;
                            this.Invalidate();
                            changed = true;
                            break;
                        }
                    }
                    if (changed == true) { break; }
                }
            }

            else if (e.KeyChar == 's')
            {
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        if (Board[i, j] == 0 && i < 3)
                        {
                            Board[i, j] = Board[(i + 1), j];
                            Board[(i + 1), j] = 0;
                            this.Invalidate();
                            changed = true;
                            break;
                        }
                    }
                    if (changed == true) { break; }
                }
            }

            else if (e.KeyChar == 'w')
            {
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        if (Board[i, j] == 0 && i > 0)
                        {
                            Board[i, j] = Board[(i - 1), j];
                            Board[(i - 1), j] = 0;
                            this.Invalidate();
                            break;
                        }
                    }
                    if (changed == true) { break; }
                }
            }

            if (CheckBoard())
            {
                MessageBox.Show("Du har klarat pusslet!");
                Close();
            }
        }
    }
}
  

